//
//  UserLondViewController.m
//  Yiqidianyin
//
//  Created by apple on 15/8/9.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "UserLondViewController.h"
#import "UserLondManager.h"
#import "MUser.h"
@interface UserLondViewController ()

@property (weak, nonatomic) IBOutlet UITextField *userNum;
@property (weak, nonatomic) IBOutlet UITextField *passWord;
@property (nonatomic,strong)UserLondManager *londManager;
@property (nonatomic,assign)int a;
@end

@implementation UserLondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.passWord.secureTextEntry = YES;
    self.a = 1;
    self.londManager = [[UserLondManager alloc]init];
    [self.userNum becomeFirstResponder];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)topbutton:(id)sender
{
    if ([self isRightTEL:self.userNum.text])
    {
        if (![self.passWord.text isEqualToString:@""])
        {
        [self.londManager londWithUserNum:self.userNum.text andPassWord:self.passWord.text BlockHandle:^(NSDictionary *dic) {
    
        NSString *str = [NSString stringWithFormat:@"%@",dic[@"status"]];
        
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        
        [user setObject:dic[@"expiresIn"] forKey:@"expiresin"];
        [user setObject:dic[@"userId"] forKey:@"userid"];
        [user setObject:dic[@"userToken"] forKey:@"usertoken"];
    
        [user synchronize];
        
        if ([str isEqualToString:@"0"])
        {
            NSUserDefaults *md5 = [NSUserDefaults standardUserDefaults];
            NSString *md5password = [md5 objectForKey:@"md5PassW"];
            
            
            NSLog(@"EEEEEEEEEEEEEEEEEEEEE%@",dic[@"userId"]);
            NSLog(@"pppppppppppppppppppppp%@",md5password);
            [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:dic[@"userId"] password:md5password completion:^(NSDictionary *loginInfo, EMError *error) {
                if (!error)
                {
                    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
                    UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Homepage"];
                    [self presentViewController:vc animated:YES completion:nil];

                }
                else
                {
                    UIAlertView *a = [[UIAlertView alloc]initWithTitle:@"提示" message:@"登陆失败" delegate:self cancelButtonTitle:@"好的" otherButtonTitles:nil, nil];
                    [a show];
                    NSLog(@"%@",error);
                }
            } onQueue:nil];
        }
        else if ([str isEqualToString:@"40005"])
        {
            [MUser alertViewShowTitle:@"提示" andMessage:@"用户不存在" andDelegate:self controltag:1000];
        }
        else if ([str isEqualToString:@"40006"])
        {
            [MUser alertViewShowTitle:@"提示" andMessage:@"账号或密码错误" andDelegate:self controltag:1000];
        }
        else
        {
           [MUser alertViewShowTitle:@"提示" andMessage:@"登录失败，请重新登录" andDelegate:self controltag:1000];
        }
        
    }];
        }
        else
        {
            [MUser alertViewShowTitle:@"提示" andMessage:@"请输入密码" andDelegate:self controltag:1000];
        }
    }
    else
    {
        [MUser alertViewShowTitle:@"提示" andMessage:@"请输入正确手机号儿" andDelegate:self controltag:1000];
    }
}
-(BOOL)isRightTEL:(NSString *)tel
{
    if (tel.length==11) {
        return YES;
    }
    return NO;
}

- (IBAction)topfanhuibutton:(id)sender
{
    UIStoryboard *s = [UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]];
    UIViewController *vc = [s instantiateViewControllerWithIdentifier:@"Enteruser"];
    [self presentViewController:vc animated:YES completion:nil];
    NSString *str = @"yes";
    [vc setValue:str forKey:@"isRun"];
}

- (IBAction)tapEyeButton:(id)sender
{
    if (self.a == 1)
    {
        self.passWord.secureTextEntry = NO;
        self.a--;
    }
    else
    {
        self.passWord.secureTextEntry = YES;
        self.a++;
    }
    
}
    
@end
